package com.zhangsan.seionor1.week3.auth;

import java.io.IOException;

import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;



/**
 * 拦截器
 * @author zhuzg
 *
 */
public class MyHandler extends TextWebSocketHandler {
	
	//@Override
	public void handleTextMessage(WebSocketSession session, TextMessage message) throws IOException {
		
		System.out.println(" MyHandler=====  获取到消息 >> " + message.getPayload());
		session.sendMessage(new TextMessage("消息已收到"));
		
		if (message.getPayload().equals("10")) {
			for (int i = 0; i < 10; i++) {
				session.sendMessage(new TextMessage("消息 -> " + i));
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	@Override
	public void afterConnectionEstablished(WebSocketSession session) throws Exception {
		session.sendMessage(new TextMessage(" MyHandler ========= afterConnectionEstablished ------------ 欢迎连接到ws服务"));
	}

	@Override
	public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
		System.out.println("MyHandler============  断开连接！");
	}
}
